﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using WindowsGame1.GameComponents;

namespace WindowsGame1
{
    
    
    class PrototypeHero : CollidableObject
    {
        public string AssetName = "SquareGuy";
        public string AttackAssetName = "SwordSlash";
        public EntityManager em;

        const int START_POSITION_X = 125;
        const int START_POSITION_Y = 245;
        const int WIZARD_SPEED = 300;
        const int MOVE_UP = -1;
        const int MOVE_DOWN = 1;
        const int MOVE_LEFT = -1;
        const int MOVE_RIGHT = 1;

        double attackTime = 0.0;

        enum State
        {
            Walking,
            Swording,
        }
        State mCurrentState = State.Walking;

        Vector2 mDirection = Vector2.Zero;
        Vector2 mSpeed = Vector2.Zero;

        KeyboardState mPreviousKeyboardState;



        public Rectangle HitBox;
        private float mScale = 1.0f;

        public Texture2D mSpriteTexture;
        public Texture2D mAttackSpriteTexture;

        //
        public PrototypeHero(EntityManager entityManager)
        {
            em = entityManager;
        }

        //Load the texture for the sprite using the Content Pipeline
        internal override void LoadContent(ContentManager theContentManager)
        {
            Position = new Vector2(START_POSITION_X, START_POSITION_Y);
            mSpriteTexture = theContentManager.Load<Texture2D>(AssetName);
            mAttackSpriteTexture = theContentManager.Load<Texture2D>(AttackAssetName);
            HitBox = new Rectangle(0, 0, (int)(mSpriteTexture.Width * mScale), (int)(mSpriteTexture.Height * mScale));
        }

        //Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.
        internal override void Update(GameTime theGameTime)
        {
            KeyboardState aCurrentKeyboardState = Keyboard.GetState();
            HitBox.Location = new Point((int)Position.X, (int)Position.Y);
            UpdateMovement(theGameTime, aCurrentKeyboardState);

            mPreviousKeyboardState = aCurrentKeyboardState;

            Position += mDirection * mSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
        }

        internal override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            // TODO: Add your drawing code here
            spriteBatch.Draw(mSpriteTexture, Position, Color.White);
        }

        private void UpdateMovement(GameTime gameTime, KeyboardState aCurrentKeyboardState)
        {
            if (mCurrentState == State.Walking)
            {
                mSpeed = Vector2.Zero;

                if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
                {
                    mSpeed.X = WIZARD_SPEED;
                    mDirection.X = MOVE_LEFT;
                    mDirection.Y = 0;
                }
                else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
                {
                    mSpeed.X = WIZARD_SPEED;
                    mDirection.X = MOVE_RIGHT;
                    mDirection.Y = 0;
                }

                if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
                {
                    mSpeed.Y = WIZARD_SPEED;
                    mDirection.X = 0;
                    mDirection.Y = MOVE_UP;
                }
                else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
                {
                    mSpeed.Y = WIZARD_SPEED;
                    mDirection.X = 0;
                    mDirection.Y = MOVE_DOWN;
                }

                if (aCurrentKeyboardState.IsKeyDown(Keys.Space) == true)
                {
                    Vector2 my_center = new Vector2(mSpriteTexture.Bounds.Center.X, mSpriteTexture.Bounds.Center.Y);
                    Vector2 center = new Vector2(mAttackSpriteTexture.Bounds.Center.X, mAttackSpriteTexture.Bounds.Center.Y);

                    em.Projectile(gameTime, 90, Position + (100 * mDirection));
                    attackTime = gameTime.TotalGameTime.TotalMilliseconds;
                    mCurrentState = State.Swording;
                }
            }
            else if (mCurrentState == State.Swording)
            {
                mSpeed = Vector2.Zero;

                if (gameTime.TotalGameTime.TotalMilliseconds - attackTime > 90)
                    mCurrentState = State.Walking;
            }
        }

        public Vector2 GetPosition()
        {
            return Position;
        }
    }
}
